package com.yanqu.road.server.manager.activity.apprentice.handler.impl;

import com.yanqu.road.entity.activity.apprentice.ApprenticeEffectBuffProject;
import com.yanqu.road.entity.activity.apprentice.ApprenticePvpResult;
import com.yanqu.road.entity.activity.apprentice.ApprenticeTalentData;
import com.yanqu.road.entity.activity.apprentice.config.ApprenticeLevelConfig;
import com.yanqu.road.entity.activity.apprentice.config.ApprenticeProjectConfig;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeBuffProjectData;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserData;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserPvpLog;
import com.yanqu.road.entity.activity.apprentice.enums.ApprenticeProjectIdEnum;
import com.yanqu.road.entity.activity.apprentice.enums.ApprenticeProjectSortEnum;
import com.yanqu.road.entity.enums.eOccupationType;
import com.yanqu.road.entity.log.cross.apprentice.LogCrossApprenticeProjectEvent;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeActivity;
import com.yanqu.road.server.manager.activity.apprentice.handler.ApprenticeProjectHandler;
import com.yanqu.road.server.manager.activity.apprentice.handler.param.*;
import com.yanqu.road.server.manager.log.CrossAutoLogMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.RandomHelper;

import java.util.*;

public class ApprenticeDoProjectType8Handler implements ApprenticeProjectHandler {

    /**
     * 天资辩学
     * @param req
     * @return
     */
    @Override
    public ApprenticeDoProjectResp doProject(ApprenticeDoProjectReq req) {
        ApprenticeProjectConfig projectConfig = req.getProjectConfig();
        CrossApprenticeActivity activity = req.getActivity();
        Map<Integer, Integer> talentAddRateMap = req.getTalentAddRateMap();
        Map<Integer, Integer> sortAddRateMap = req.getSortAddRateMap();
        String[] params = projectConfig.getParam().split("\\|");
        int pvpNum = Integer.parseInt(params[0]);//辩学次数
        int needWinNum = Integer.parseInt(params[1]);//胜利次数
        long winReward = Long.parseLong(params[2]);//胜利奖励天资
        long loseReward = Long.parseLong(params[3]);//失败奖励天资
        //我方玩家
        ApprenticeUserData userData = req.getUserData();
        ApprenticeLevelConfig levelConfig = activity.getConfig().getLevelConfig(userData.getLevel());
        if (levelConfig == null) {
            return null;
        }
        //判断是否有BUFF
        boolean isHaveBuff = false;
        for (ApprenticeBuffProjectData buffProjectData : userData.getBuffProjectIds()) {
            if (buffProjectData.getProjectId() == ApprenticeProjectIdEnum.PROJECT_ID_10027.getId() && buffProjectData.getNum() > 0) {
                isHaveBuff = true;
                buffProjectData.addNum(-1);
                break;
            }
        }
        if (!isHaveBuff) {
            return null;
        }
        //对方玩家
        ApprenticeUserData targetUserData = activity.getPvpUserData(userData);
        if (targetUserData == null) {
            return null;
        }
        UserBaseInfo targetBaseInfo = CrossUserMgr.getUserBaseInfo(targetUserData.getUserId());
        if (targetBaseInfo == null) {
            return null;
        }
        //获取我方最高天资
        int maxTalentType = userData.getMaxTalentType();
        //再随机出其他的天资
        Set<Integer> talentSet = eOccupationType.getOccuptionTypeSet();
        talentSet.remove(maxTalentType);
        Set<Integer> pvpTalentSet = RandomHelper.selectRandomElements(talentSet, pvpNum - 1);
        pvpTalentSet.add(maxTalentType);
        //对比
        int winNum = 0;
        List<ApprenticeTalentData> myTalents = new ArrayList<>();
        List<ApprenticeTalentData> targetTalents = new ArrayList<>();
        for (int type : pvpTalentSet) {
            //我方
            long talentVale = userData.getTalentByType(type);
            myTalents.add(new ApprenticeTalentData(type, talentVale));
            //对方
            long targetTalentVale = targetUserData.getTalentByType(type);
            targetTalents.add(new ApprenticeTalentData(type, targetTalentVale));
            if (talentVale >= targetTalentVale) {
                winNum++;
            }
        }
        //天资奖励
        boolean winFlag = winNum >= needWinNum;
        long talentReward = winFlag ? winReward : loseReward;
        //随机增加天资
        int talentType = new Random().nextInt(5) + 1;
        //加成计算
        talentReward = activity.countTalentValue(talentType, ApprenticeProjectSortEnum.SORT_TYPE_0.getType(), talentReward, talentAddRateMap, sortAddRateMap, levelConfig.getConsume());
        //增加天资
        ApprenticeTalentData addTalent = userData.addTalent(talentType, talentReward, true);
        //日志
        ApprenticeUserPvpLog pvpLog = new ApprenticeUserPvpLog(
                userData.getUserId(),
                targetUserData.getUserId(),
                winFlag,
                addTalent,
                myTalents,
                targetTalents
        );
        userData.addPvpLog(pvpLog);//我方加日志
        targetUserData.addPvpLog(pvpLog);//对方加日志
        //同步一下对方
        activity.syncUserData(targetUserData.getUserId());
        //日志
        LogCrossApprenticeProjectEvent log = new LogCrossApprenticeProjectEvent(
                userData.getActivityId(),
                userData.getUserId(),
                userData.getProgramNum(),
                String.valueOf(ApprenticeProjectIdEnum.PROJECT_ID_10027.getId()),
                targetUserData.getUserId(),
                winFlag ? 1 : 0,
                addTalent.talentToString()
        );
        CrossAutoLogMgr.add(log);
        //生效buff结果结果集注入
        List<ApprenticeEffectBuffProject> effectBuffProjects = req.getEffectBuffProjects();
        ApprenticeEffectBuffProject effectBuffProject = new ApprenticeEffectBuffProject(projectConfig.getId());
        effectBuffProjects.add(effectBuffProject);
        //返回
        ApprenticePvpResult result = new ApprenticePvpResult();
        result.setMyTalents(myTalents);
        result.setTargetTalents(targetTalents);
        result.setWinNum(winNum);
        result.setAddTalent(addTalent);
        result.setTargetUserName(targetBaseInfo.getNickName());
        result.setTargetUserLevel(targetUserData.getLevel());
        result.setAddRate(talentAddRateMap.getOrDefault(talentType, 0) + sortAddRateMap.getOrDefault(ApprenticeProjectSortEnum.SORT_TYPE_0.getType(), 0));
        ApprenticeDoProjectResp resp = new ApprenticeDoProjectResp();
        resp.setPvpResult(result);
        return resp;
    }

    @Override
    public void countTalentTypeAddRate(ApprenticeCountTalentAddRateReq req) {

    }

    @Override
    public void countSortTypeAddRate(ApprenticeCountSortAddRateReq req) {

    }
}
